package engine.content;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

import org.apache.commons.io.IOUtils;




public class FileContent implements IContent<InputStream, File> {

	private File file;
	private String contentType, contentBase;
	private String suffix;
	private String description;
	
	@Override
	public File getContent() {
		return file;
	}

	@Override
	public ContentTypes getType() {
		return ContentTypes.FILE;
	}

	@Override
	public void setContent(InputStream content) {
		try {
			suffix = contentType.substring(contentType.indexOf("/") +1);

			file = File.createTempFile("tmp", "."+suffix);
			file.deleteOnExit();

	        OutputStream outStream = new FileOutputStream(file);
	        IOUtils.copy(content, outStream);
	        outStream.close();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}

	}
	
	@Override
	public String getContentType() {
		return contentType;
		
	}

	@Override
	public void setContentType(String contentTypeStr) {
		this.contentType = contentTypeStr;
	}
	
	@Override
	public String getContentBase() {
		return contentBase;
		
	}

	@Override
	public void setContentBase(String contentBaseStr) {
		this.contentBase = contentBaseStr;
	}

	@Override
	public String getContentAsString() {
		throw new RuntimeException("not supported");
	}
	
	@Override
	public String getFileSuffix() {
		return suffix;
	}

	@Override
	public String getDescription() {
		return this.description;
	}

	@Override
	public void setDescription(String setDescription) {
		this.description = setDescription;		
	}
}
